package com.stu.info;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.stu.aq.*;
import com.stu.usb.SerialIOManager;
import com.stu.widget.*;

public class MotorCommandsFragment extends CommandFragment implements OnClickListener, MotorBarView.Listener,  NumberEntryPopup.NumberEntryListener, View.OnTouchListener {
	private MotorBarView motorBar[];
	private EditText text[];
	private int scaleFactor;
	private SerialIOManager ioManager;
	private boolean warnFlag;

	public MotorCommandsFragment() {
		motorBar = new MotorBarView[AQ.motorCount];
		text = new EditText[AQ.motorCount];
		scaleFactor = (AQ.motorMax - AQ.motorMin) / 100;
		warnFlag = false;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	  Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.motor_commands, container, false);
		motorBar[0] = (MotorBarView)v.findViewById( R.id.motorCommandBar1 );
		motorBar[0].setListener( this );
		motorBar[1] = (MotorBarView)v.findViewById( R.id.motorCommandBar2 );
		motorBar[1].setListener( this );
		motorBar[2] = (MotorBarView)v.findViewById( R.id.motorCommandBar3 );
		motorBar[2].setListener( this );
		motorBar[3] = (MotorBarView)v.findViewById( R.id.motorCommandBar4 );
		motorBar[3].setListener( this );
		String val = String.valueOf( AQ.motorMin );
		text[0] = (EditText)v.findViewById( R.id.motorText1 );
		text[0].setText( val );
		text[0].setTag( 0 );
		text[0].setOnTouchListener( this );
		text[1] = (EditText)v.findViewById( R.id.motorText2 );
		text[1].setText( val );
		text[1].setOnTouchListener( this );
		text[1].setTag( 1 );
		text[2] = (EditText)v.findViewById( R.id.motorText3 );
		text[2].setText( val );
		text[2].setOnTouchListener( this );
		text[2].setTag( 2 );
		text[3] = (EditText)v.findViewById( R.id.motorText4 );
		text[3].setText( val );
		text[3].setOnTouchListener( this );
		text[3].setTag( 3 );
		Button b = (Button)v.findViewById( R.id.motorButton1 );
		b.setOnClickListener( this );
		b = (Button)v.findViewById( R.id.motorButton2 );
		b.setOnClickListener( this );
		b = (Button)v.findViewById( R.id.motorButton3 );
		b.setOnClickListener( this );
		b = (Button)v.findViewById( R.id.motorButton4 );
		b.setOnClickListener( this );
		b = (Button)v.findViewById( R.id.sendButton );
		b.setEnabled( AQ.connected );
		b.setOnClickListener( this );
		b = (Button)v.findViewById( R.id.stopButton );
		b.setEnabled( AQ.connected );
		b.setOnClickListener( this );
		b = (Button)v.findViewById( R.id.escMaxButton );
		b.setEnabled( AQ.connected );
		b.setOnClickListener( this );
		b = (Button)v.findViewById( R.id.escMinButton );
		b.setEnabled( AQ.connected );
		b.setOnClickListener( this );
		b = (Button)v.findViewById( R.id.escEndButton );
		b.setEnabled( AQ.connected );
		b.setOnClickListener( this );
		return v;
	}

	@Override
	public void onClick(View v) {
		int ix = -1;
		switch( v.getId() ) {
			case R.id.motorButton1:
				ix = 0;
				break;
			case R.id.motorButton2:
				ix = 1;
				break;
			case R.id.motorButton3:
				ix = 2;
				break;
			case R.id.motorButton4:
				ix = 3;
				break;
			case R.id.sendButton:
				sendMotors();
				break;
			case R.id.stopButton:
				stopMotors();
				break;
			case R.id.escMaxButton:
				sendEsc( 0 );
				break;
			case R.id.escMinButton:
				sendEsc( 1 );
				break;
			case R.id.escEndButton:
				sendEsc( 2 );
				break;
		}
		if( ix != -1 ) {
			text[ix].setText( String.valueOf(AQ.motorTest) );
			motorBar[ix].setValue( (AQ.motorTest - AQ.motorMin) / scaleFactor );
		}
	}

	@Override
	public void OnValueChanged(int id, int value) {
		int ix = -1;
		switch( id ) {
			case R.id.motorCommandBar1:
				ix = 0;
				break;
			case R.id.motorCommandBar2:
				ix = 1;
				break;
			case R.id.motorCommandBar3:
				ix = 2;
				break;
			case R.id.motorCommandBar4:
				ix = 3;
				break;
		}
		text[ix].setText( String.valueOf((value * scaleFactor) + AQ.motorMin) );
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if( event.getActionMasked() == MotionEvent.ACTION_UP ) {
			((AQ)getActivity()).showEntryDialog( this, (EditText)v, true );
		} else if( event.getActionMasked() == MotionEvent.ACTION_DOWN )
			v.requestFocus();
		return true;
	}
	
	@Override
	public void OnDone( EditText t ) {
		int scaledVal, val;
		
		int ix = (Integer)t.getTag();
		String s = t.getText().toString();
		if( s.isEmpty() ) {
			// use current value
			scaledVal = motorBar[ix].getValue();
			val = (scaledVal * scaleFactor) + AQ.motorMin;
		} else {
			try {
				val = Integer.parseInt( s );
				if( val < AQ.motorMin )
					val = AQ.motorMin;
				if( val > AQ.motorMax )
					val = AQ.motorMax;
				scaledVal = (val - AQ.motorMin) / scaleFactor;
			} catch( NumberFormatException e ) {
				// use current value
				scaledVal = motorBar[ix].getValue();
				val = (scaledVal * scaleFactor) + AQ.motorMin;
			}
			motorBar[ix].setValue( scaledVal );
		}
		t.setText( String.valueOf(val) );
		t.selectAll();
	}

	private void stopMotors() {
		String s = new String( "5123.45;" );
		for( int i = 0; i < AQ.motorCount; i++ ) {
			motorBar[i].setValue( 0 );
			text[i].setText( String.valueOf(AQ.motorMin) );
			s += String.valueOf(AQ.motorMin) + ";";
		}
		if( AQ.connected )
			ioManager.writeAsync( s.getBytes() );
		Button b = (Button)getView().findViewById( R.id.stopButton );
		b.setEnabled( false );
	}

	private void sendMotors() {
		String s = new String( "5123.45;" );
		for( int i = 0; i < AQ.motorCount; i++ ) {
			s += String.valueOf((motorBar[i].getValue() * scaleFactor) + AQ.motorMin) + ";";
		}
		if( AQ.connected )
			ioManager.writeAsync( s.getBytes() );
		Button b = (Button)getView().findViewById( R.id.stopButton );
		b.setEnabled( true );
	}

	private void sendEsc( int v ) {
		if( AQ.connected ) {
			if( v == 0 && !warnFlag ) {
				warnFlag = true;
				AlertDialog.Builder b = new AlertDialog.Builder( getActivity() );
				b.setTitle( "ESC Warning" );
				b.setMessage( "Make Sure Battery is Disconnected" );
				b.setPositiveButton( android.R.string.yes, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						sendEscMaxString();
					}
				});
				b.setNegativeButton( android.R.string.no, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				});
				b.create().show();
			} else {
				String s = new String();
				switch( v ) {
					case 0:
						s = "1";	// 2000
						break;
					case 1:
						s = "2";	// 1000
						break;
					case 2:
						s = "4";	// 1200
						break;
				}
				s += "123.45";
				ioManager.writeAsync( s.getBytes() );
			}
		}
	}

	private void sendEscMaxString() {
		String s = "1123.45";
		ioManager.writeAsync( s.getBytes() );
	}
	
	@Override
	public void update(OnCommandListener l, SerialIOManager io) {
		ioManager = io;
		Button b = (Button)getView().findViewById( R.id.sendButton );
		b.setEnabled( true );
		b = (Button)getView().findViewById( R.id.escMaxButton );
		b.setEnabled( true );
		b = (Button)getView().findViewById( R.id.escMinButton );
		b.setEnabled( true );
		b = (Button)getView().findViewById( R.id.escEndButton );
		b.setEnabled( true );
	}

	@Override
	public void updateUI(Object statusData) {
	}

	@Override
	public void stopUpdate(OnCommandListener l, SerialIOManager io) {
		Button b = (Button)getView().findViewById( R.id.sendButton );
		b.setEnabled( false );
		b = (Button)getView().findViewById( R.id.stopButton );
		b.setEnabled( false );
		b = (Button)getView().findViewById( R.id.escMaxButton );
		b.setEnabled( false );
		b = (Button)getView().findViewById( R.id.escMinButton );
		b.setEnabled( false );
		b = (Button)getView().findViewById( R.id.escEndButton );
		b.setEnabled( false );
		l.OnCommandComplete( getId(), FragmentState.STOPPING );
	}

	@Override
	public void clear() {
		for( int i = 0; i < AQ.motorCount; i++ ) {
			motorBar[i].setValue( 0 );
			text[i].setText( "0" );
		}
		Button b = (Button)getView().findViewById( R.id.sendButton );
		b.setEnabled( false );
		b = (Button)getView().findViewById( R.id.stopButton );
		b.setEnabled( false );
		b = (Button)getView().findViewById( R.id.escMaxButton );
		b.setEnabled( false );
		b = (Button)getView().findViewById( R.id.escMinButton );
		b.setEnabled( false );
		b = (Button)getView().findViewById( R.id.escEndButton );
		b.setEnabled( false );
	}

	@Override
	public void write(OnCommandListener l, SerialIOManager io) {
	}
}
